package controllers;

import java.awt.event.ActionEvent;
import java.util.Vector;

import dialogs.DeleteTupleDialog;
import dialogs.InsertTupleDialog;

import models.ShipmentModel;

import system.View;

public class ShipmentController extends AbstractMenuItemController {
	private ShipmentModel shipment = new ShipmentModel();

	public ShipmentController(View v) {
		super(v);
		abstractModel = shipment;
		shipment.addExceptionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String actionCommand = e.getActionCommand();

		if (actionCommand.equals("Delete Shipment"))
			showDialogWindow(new DeleteTupleDialog(view, actionCommand, "Key",
					new String[] { "Shipment ID Number: " }, this));
		else if (actionCommand.equals("Insert Shipment"))
			showDialogWindow(new InsertTupleDialog(view, actionCommand,
					"Shipment", new String[] { "Supplier Name: ",
							"Destination Store: ", }, this));
	}

	@Override
	public int validateInsert(Vector<String> s) {
		if (s.size() != 2)
			return VALIDATIONERROR;

		try {
			String supName;
			String storeName;

			if (s.get(0).trim().length() != 0)
				supName = s.get(0).trim();
			else return VALIDATIONERROR;
			if (s.get(1).length() != 0)
				storeName = s.get(1).trim();
			else return VALIDATIONERROR;

			view.updateStatusBar("Inserting Shipment from Supplier to Store...");

			if (shipment.insert(supName, storeName)) return operationSuccess();
			else return operationFailure();
		} catch (NumberFormatException ex) {
			return VALIDATIONERROR;
		}
	}
	
	@Override
	public int validateDelete(Vector<String> s) {
		if (s.size() != 1)
			return VALIDATIONERROR;

		try {
			Integer sid;

			if (s.get(0).trim().length() != 0)
				sid = Integer.valueOf(s.get(0).trim());
			else return VALIDATIONERROR;

			view.updateStatusBar("Deleting Shipment...");

			if (shipment.delete(sid)) return operationSuccess();
			else return operationFailure();
		} catch (NumberFormatException ex) {
			return VALIDATIONERROR;
		}
	}
}
